package com.zjitc.controller;

import com.zjitc.common.ServerResponse;
import com.zjitc.model.Emp;
import com.zjitc.redis.MyRedis;
import com.zjitc.service.impl.UserServiceImpl;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import com.zjitc.model.User;
import com.zjitc.token.Token;
import org.junit.BeforeClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zjitc.service.UserService;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/28 14:04
 * @description:
 */
@Controller
@RequestMapping("/user")
public class UserController {

  @Autowired
  private UserService userService;
  private ServerResponse response;

  public static Jedis jedis;
  @BeforeClass
  public static void init() {
    jedis = new Jedis();
    System.out.println("连接成功");
    System.out.println("服务正在运行：" + jedis.ping());
  }

  /**
   * 2.4.1 登录
   */
  @RequestMapping("/login")
  @ResponseBody
  public ServerResponse login(
      @RequestParam(name = "username") String username,
      @RequestParam(name = "password") String password, HttpSession session) {
    ServerResponse<User> response = userService.login(username, password);
    if (response.success()) {
      session.setAttribute("user", response.getData());
    }
    return response;
  }

  /**
   * 2.4.2 注册
   */
  @RequestMapping("/register")
  @ResponseBody
  public ServerResponse register(User user, HttpSession session) {
    ServerResponse<User> response = userService.userRegister(user);
    if (response.success()) {
      session.setAttribute("user", response.getData());
      Jedis redis = new Jedis();
      redis.set(String.valueOf(user.id), String.valueOf(user));
      redis.get(user.username);
    }
    return response;
  }

  /**
   * 2.4.3 检查用户名是否有效
   */
  @RequestMapping("/check_valid")
  @ResponseBody
  public ServerResponse check_valid(String str, String type) {
    ServerResponse response = userService.check_valid(str, type);
    return response;
  }

  /**
   * 2.4.4 获取登录用户信息
   */
  @RequestMapping("/get_user_info")
  @ResponseBody
  public ServerResponse get_user_info(HttpSession session) {
    User user = (User) session.getAttribute("user");
    ServerResponse response = userService.get_user_info(user);
    return response;
  }

  /**
   * 2.4.5 忘记密码
   */
  @RequestMapping("/forget_get_question")
  @ResponseBody
  public ServerResponse forget_get_question(@RequestParam(name = "username") String username) {
    ServerResponse response = userService.forget_get_question(username);
    return response;
  }

  /**
   *
   */
  @RequestMapping("/forget_check_answer")
  @ResponseBody
  public ServerResponse forget_check_answer(@RequestParam(name = "username") String username,
      @RequestParam(name = "question") String question,
      @RequestParam(name = "answer") String answer) {
    ServerResponse response = userService.forget_check_answer(username, question, answer);
/*    Token token = new Token();
    Jedis jedis = new Jedis();
    jedis.set()*/
    return response;
  }

  /**
   *
   */
  @RequestMapping("/forget_reset_password")
  @ResponseBody
  public ServerResponse forget_reset_password(String username, String passwordOld, Token forgetToken) {

    ServerResponse response = userService.forget_reset_password(username,passwordOld,forgetToken);
    return response;
  }

  /**
   * 2.4.8 登录中状态重置密码
   */
  @RequestMapping("/reset_password")
  @ResponseBody
  public ServerResponse reset_password(String passwordOld, String passwordNew,
      HttpSession session) {
    ServerResponse response = null;
    try {
      User user = (User) session.getAttribute("user");
      response = userService.reset_password(user, passwordOld, passwordNew);
      return response;
    } catch (Exception e) {
      return ServerResponse.createError("用户未登录,无法获取当前用户信息");
    }
  }

  /**
   * 2.4.9 登录状态更新个人信息
   */
  @RequestMapping("/update_information")
  @ResponseBody
  public ServerResponse update_information(String email, String phone, String question,
      String answer, HttpSession session) {
    try {
      User user = (User) session.getAttribute("user");
      ServerResponse response = userService
          .update_information(user, email, phone, question, answer);
      return response;
    } catch (Exception e) {
      return ServerResponse.createError("用户未登录,无法获取当前用户信息");
    }
  }

  /**
   * 2.4.10 获取当前登录用户的详细信息，并强制登录
   */
  @RequestMapping("/get_information")
  @ResponseBody
  public ServerResponse get_information(HttpSession session) {
    try {
      User user = (User) session.getAttribute("user");
      ServerResponse response = userService.get_information(user);
      return response;
    } catch (Exception e) {
      return ServerResponse.createError(10, "用户未登录,无法获取当前用户信息");
    }
  }

  /**
   * 2.4.11 退出登录
   */
  @RequestMapping("/logout")
  @ResponseBody
  public ServerResponse logout(HttpSession session) {
    try {
      User user = (User) session.getAttribute("user");
      if (user.getUsername() != null) {
        session.removeAttribute("user");
        return ServerResponse.createSuccess("退出成功");
      }
    } catch (Exception e) {
      return ServerResponse.createError("服务端异常");
    }
    return response;
  }
  @RequestMapping("/add")
  @ResponseBody
  public ServerResponse Emp(){
    List<Emp> list = new ArrayList<>();
    response = userService.Emp(list);
  return response;
  }
}
